package com.tushen.datacenter.ts.data;

import android.os.Handler;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.common.util.Event;
import com.common.util.OkHttpUtil;
import com.common.util.UIHandler;
import com.common.util.Util;

import org.greenrobot.eventbus.EventBus;

public class DataManager {

    protected static final String TAG = "DataManager";

    public static Boolean IS_TEST = false;
    public static String PROJECT_ID = "1586449037520";
    public static String TANANT_ID = "1655802128272";
    public static String TOKEN = "";

    private static DataManager mInstance = null;
    private Thread mDataThread = null;

    private DataListener.Data0Listener mListener0;
    private DataListener.Data1Listener mListener1;
    private DataListener.Data2Listener mListener2;
    private DataListener.Data3Listener mListener3;
    private DataListener.Data4Listener mListener4;
    private DataListener.Data5Listener mListener5;
    private DataListener.Data6Listener mListener6;
    private DataListener.Data5sListener mXsListener;
    private Handler mHandler;

    private boolean mRunning = false;

    private DataManager() {

    }


    public static DataManager getInstance() {

        if (mInstance == null) {
            synchronized (DataManager.class) {
                if (mInstance == null) {
                    mInstance = new DataManager();
                }
            }
        }
        return mInstance;
    }

    public void start() {
        stop();
        mRunning = true;
        mDataThread = new Thread(new DataTask());
        mDataThread.start();

    }

    public void stop() {

        mRunning = false;
        try {
            if (mDataThread != null) {
                mDataThread.interrupt();
                mDataThread.join(1000);
            }
        } catch (Exception e) {
            ;
        }
        mDataThread = null;
    }

    public void setmListener1(DataListener.Data1Listener mListener1) {
        this.mListener1 = mListener1;
    }

    public void setmListener2(DataListener.Data2Listener mListener2) {
        this.mListener2 = mListener2;
    }

    public void setmListener3(DataListener.Data3Listener mListener3) {
        this.mListener3 = mListener3;
    }

    public void setmListener4(DataListener.Data4Listener mListener4) {
        this.mListener4 = mListener4;
    }

    public void setmListener5(DataListener.Data5Listener mListener5) {
        this.mListener5 = mListener5;
    }

    public void setmListener6(DataListener.Data6Listener mListener6) {
        this.mListener6 = mListener6;
    }


    public Handler getmHandler() {
        return mHandler;
    }

    public void setmHandler(Handler mHandler) {
        this.mHandler = mHandler;
    }

    private long lastGetData1Time = 0;
    private long lastGetData2Time = 0;
    private long lastGetData3Time = 0;
    private long lastGetData4Time = 0;
    private long lastGetData5Time = 0;
    private long lastGetData6Time = 0;
    private long lastXsNotifyTime = 0;
    private long lastGetTokenTime = 0;

    public void resetTime(){
        lastGetData1Time = 0;
        lastGetData2Time = 0;
        lastGetData3Time = 0;
        lastGetData4Time = 0;
        lastGetData5Time = 0;
        lastGetData6Time = 0;
        lastXsNotifyTime = 0;
    }

    public void setmListener0(DataListener.Data0Listener mListener0) {
        this.mListener0 = mListener0;
    }

    public void setmXsListener(DataListener.Data5sListener mXsListener) {
        this.mXsListener = mXsListener;
    }

    private class DataTask implements Runnable {
        @Override
        public void run() {
            Log.d(TAG, "DataTask start...");


            while (mRunning) {

                try {
                    Thread.sleep(200);
                    long now = System.currentTimeMillis();

                    if(Util.isEmpty(TOKEN)){
                        TOKEN = DataUtil.getToken();
                        lastGetTokenTime = now;
                    }
                    if(Util.isEmpty(PROJECT_ID) || Util.isEmpty(TANANT_ID) || Util.isEmpty(TOKEN)){
                        continue;
                    }
                    //Log.d(TAG, "DataTask running.."+DateUtil.getNowDateStr());
                    if(now - lastGetTokenTime > 24*3600*1000L){
                        TOKEN = DataUtil.getToken();//24小时获取一次新的token
                        lastGetTokenTime = now;
                    }
                    if ( now - lastGetData1Time > 2000 && getData1()) {
                        lastGetData1Time = now;
                    }

                    if (now - lastGetData3Time > 10*1000 && getData3()) {
                        lastGetData3Time = now;
                    }
                    if (now - lastGetData4Time > 3600*1000 && getData4()) {
                        lastGetData4Time = now;
                    }
                    if (now - lastGetData5Time > 3600*1000 && getData5()) {
                        lastGetData5Time = now;
                    }
                    if (now - lastGetData6Time > 10*1000 && getData6()) {
                        lastGetData6Time = now;
                    }

                    if (now - lastGetData2Time > 10*1000 && getData2() && getData0()) {
                        lastGetData2Time = now;
                    }

                    if(now - lastXsNotifyTime > 10*1000 && mXsListener != null){
                        lastXsNotifyTime = now;
                        UIHandler.getInstance().post(mHandler, new Runnable() {
                            @Override
                            public void run() {
                                mXsListener.onXSecondPassed();
                            }
                        });
                    }

                } catch (InterruptedException e) {
                    Log.d(TAG, "DataTask catch InterruptedException..break..");
                    break;
                } catch (Exception e) {
                    Log.e(TAG, "DataTask catch Exception e = " + e.getClass().getSimpleName());
                    e.printStackTrace();
                }
            }
            Log.d(TAG, "DataTask end...");
        }
    }

    public boolean checkNetWork() {

        boolean ret = OkHttpUtil.executeGet("http://www.baidu.com", null,null) == null ? false : true;

        EventBus.getDefault().post(new Event.NetWorkChangeEvent(ret));

        return ret;
    }

    private boolean getData0() {

        try {
            if(mListener0 == null)
                return true;

            final JSONObject jsonObject = DataUtil.getdata0(DataManager.PROJECT_ID);
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener0.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData0 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean getData1() {

        try {
            if(mListener1 == null)
                return true;

            final JSONObject jsonObject = DataUtil.getdata1();
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener1.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData1 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean getData2() {

        try {
            if(mListener2 == null)
                return true;

            final JSONObject jsonObject = DataUtil.getdata2();
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener2.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData2 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean getData3() {

        try {
            if(mListener3 == null)
                return true;

            final JSONObject jsonObject = DataUtil.getdata3();
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener3.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData2 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean getData4() {

        try {
            if(mListener4 == null)
                return true;

            final JSONObject jsonObject = DataUtil.getdata4();
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener4.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData4 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean getData5() {

        try {
            if(mListener5 == null)
                return true;

            final  JSONObject jsonObject = DataUtil.getdata5();
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener5.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData5 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean getData6() {

        try {
            if(mListener6 == null)
                return true;

            final JSONObject jsonObject = DataUtil.getdata6();
            if(!checkVaild(jsonObject)){
                return false;
            }

            UIHandler.getInstance().post(mHandler, new Runnable() {

                @Override
                public void run() {

                    mListener6.onDataChanged(jsonObject);

                }
            });

            return true;
        } catch (Exception e) {
            Log.e(TAG, "getData6 catch Exception e = " + e.getClass().getSimpleName());
            e.printStackTrace();
        }
        return false;
    }

    private boolean checkVaild(JSONObject jsonObject){

        if (jsonObject == null || jsonObject.getInteger("code") == null  || jsonObject.getInteger("code") != 200) {

            if(jsonObject != null && jsonObject.getString("error") != null
                    && "invalid_token".equals(jsonObject.getString("error"))){
                TOKEN = DataUtil.getToken();
            }
            return false;
        }

        return true;
    }


}
